// pages/search/search.js
var next_page=0,page=1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list_show: false,
    inp_val: "",
    search_data: '',
    search_type: 1,
    list: [],
    shut_cont: false
  },


  onLoad: function (t) {
    console.log(t,'onload')
    getApp().page.onLoad(this, t),page=1;
    wx.setNavigationBarTitle({
      title: t.search_topic
    })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    // getApp().core.getStorageSync(getApp().const.USERREGION) && (s.setData({
    //   region: getApp().core.getStorageSync(getApp().const.USERREGION)
    // }));
    if (t.search_data) {
      this.setData({
        search_data: t.search_data
      });
      this.searchConfirm();
    }
    if (t.search_topic) {
      this.setData({
        search_data: t.search_topic,
        search_type: 2
      });
      this.searchConfirm();
    }


  },
  kind_select() {
    this.setData({
      list_show: !this.data.list_show
    })
  },
  inp_cont(e) {
    this.setData({
      search_data: e.detail.value,
      page: 1,
    })
  },
  chageSearchType(e) {
    this.setData({
      search_type: e.currentTarget.dataset.type == 2 ? 2 : 1
    });
  },
  searchConfirm() {
    var t = this;
    getApp().core.showLoading({}), getApp().request({
      url: getApp().api.weitao.search_data,
      data: {
        page: page,
        search_data: this.data.search_data,
        search_type: this.data.search_type,
      },
      method: "get",
      success: function (e) {
        getApp().core.hideLoading()
        if (0 == e.code) {
          var i = t.data.list,
            s = e.data.list,
            n = !1;
          10 == s.length && (n = 1), i = i.concat(s), page++, t.setData({
            list: i,
            user: e.data.user
          });
          
          next_page = n;
        } else getApp().core.showToast({
          title: e.msg,
          icon: 'none'
        });
      },
      fali: function (t) {
        getApp().core.hideLoading()
        getApp().core.showToast({
          title: t.msg,
          icon: 'none'
        });
      }
    });
  },
  onShareAppMessage: function (res) {
    getApp().page.onShareAppMessage(this);
    if (res.from === 'button') {
      var article = res.target.dataset.article;
      return {
        title: article.content,
        path: '/weitao/details/details?article_id=' + article.id + '&store_id=' + (wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0),
        success: function (res) {
          console.log('成功', res)
        }
      }
    }
    return {
      title: '微淘搜索',
      path: '/weitao/search_list/search_list',
      success: function (res) {
        console.log('成功', res)
      }
    }
  },
  shut_inp() {
    console.log("1111111")
    this.setData({
      inp_val: "",
      shut_cont: false
    })
  },
  //跳转页面
  goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url;
    if (!gopage) {
      return;
    }
    getApp().core.navigateTo({
      url: gopage
    });
  },
  show_detail(e) {
    if (e.currentTarget.dataset.article && this.data.is_detail_show != 1) {
      article = e.currentTarget.dataset.article;
      article.head_url = article.from_user_head;
      article.nickname = article.from_user_name;
      article.is_user_liked = article.is_focused;
    }
    this.setData({
      article: article,
      is_detail_show: this.data.is_detail_show == 1 ? 0 : 1
    });
  },
  like_action(e) {
    var user_index = e.currentTarget.dataset.userIndex,
      s = this,
      article = s.data.list[user_index];
    var like_data = 0;
    article.is_liked == 1 ? like_data = 0 : like_data = 1;
    let like_count = article.like_count;
    if (article.id > 0) {
      var a = this;
      getApp().core.showLoading({
        title: "加载中"
      }), getApp().request({
        url: getApp().api.weitao.like_article,
        data: {
          article_id: article.id,
          is_liked: like_data
        },
        success: function (t) {
          if (t.code == 0) {
            s.data.list[user_index].is_liked = like_data;
            if (like_data == 1) {
              s.data.list[user_index].like_count = parseInt(like_count) + 1;
            } else {
              if (parseInt(like_count) > 1) {
                s.data.list[user_index].like_count = parseInt(like_count) - 1;
              } else {
                s.data.list[user_index].like_count = 0;
              }
            }
            a.setData({
              list: s.data.list
            });
          }
          getApp().core.showToast({
            title: t.msg,
            icon: 'none'
          });
        },
        complete: function () {
          getApp().core.hideLoading();
        }
      });
    } else {
      getApp().core.showToast({
        title: "系统错误",
        icon: 'none'
      });
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(next_page);
    next_page == 1 && this.searchConfirm()
  },
})